package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.constant.ReturnValueConstants;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.TDept;
import com.emily.test.service.DeptService;
import com.emily.test.service.RedisService;
import com.emily.test.utils.HeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author ：zc
 * @date ：Created in 2019/3/27 13:48
 * @description：部门接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "dept")
@Api(value = "DeptApi", description = "部门接口类", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class DeptController {
    private Logger logger = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    DeptService deptService;
    @Autowired
    private RedisService redisService;

    @ApiOperation(value="获取所有的部门列表", notes="获取所有的部门列表",httpMethod="POST")
    @PostMapping(value = "getDeptAllList")
    public ResponseResult getDeptAllList(){
        try{
            return ResponseResult.success("0",ReturnValueConstants.QUERY_SUCCESS,deptService.getDeptAllList());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.QUERY_FAILURE);
        }
    }

    @ApiOperation(value="获取一个公司的部门列表,为了展示树结构", notes="获取一个公司的部门列表,为了展示树结构",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "公司code", required = true)
    })
    @PostMapping(value = "getDeptListToTree")
    public ResponseResult getDeptListToTree(HttpServletRequest request){
        try {
            String companyCode = HeaderUtil.getCompanyCode(request,redisService);
            logger.info("公司code为{}",companyCode);
            List<Map<String,Object>> mapList= deptService.getDeptMapListToTree(companyCode);
            logger.info("获取部门信息成功:{}",mapList);
            return ResponseResult.success("0",ReturnValueConstants.QUERY_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.QUERY_FAILURE);
        }
    }

    @ApiOperation(value="获取一个公司的部门列表", notes="根据公司code获取该公司名下的部门列表",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "公司code", required = true)
    })
    @PostMapping(value = "getDeptList")
    public ResponseResult getDeptList(HttpServletRequest request){
        try {
            String companyCode = HeaderUtil.getCompanyCode(request,redisService);
            logger.info("公司code为{}",companyCode);
            List<Map<String,Object>> mapList= deptService.getDeptMapList(companyCode);
            logger.info("获取部门信息成功:{}",mapList);
            return ResponseResult.success("0",ReturnValueConstants.QUERY_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.QUERY_FAILURE);
        }
    }
    @ApiOperation(value="获取一个部门信息", notes="根据部门id获取该公司名下的部门列表",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", required = true)
    })
    @PostMapping(value = "getDept")
    public ResponseResult getDept(@RequestBody JSONObject jsonObject){
        try {
            Integer id = jsonObject.getInteger("id");
            logger.info("查询id为{}的部门 ",id);
            TDept dept=deptService.getDept(id);
            logger.info("查询部门信息成功：{} ",dept);
            return ResponseResult.success("0",ReturnValueConstants.QUERY_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.QUERY_FAILURE);
        }
    }
    @ApiOperation(value="删除部门信息", notes="根据部门id删除部门信息",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "部门id", required = true)
    })
    @PostMapping(value = "delDept")
    public ResponseResult delDept(@RequestBody JSONObject jsonObject){
        try {
            logger.info("删除id为{}的部门 ",jsonObject.toJSONString());
            String[] ids = jsonObject.getString("ids").replaceAll("\\[", "").replaceAll("]", "").replaceAll(" ","").split(",");
            for (int i = 0; i < ids.length; i++) {
                String a = ids[i];
                deptService.delete(Integer.parseInt(ids[i]));
            }

            return ResponseResult.success(ReturnValueConstants.DELATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.DELATE_FAILURE);
        }
    }
    @ApiOperation(value="增加部门", notes="增加一个部门信息",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyCode", value = "公司code", required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", required = true),
            @ApiImplicitParam(name = "pid", value = "父节点", required = true),
            @ApiImplicitParam(name = "description", value = "备注", required = true)
    })
    @PostMapping(value = "addDept")
    public ResponseResult addDept(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        try {
            logger.info("增加部门信息{}",jsonObject.toJSONString());
            String deptName = jsonObject.getString("deptName");
            Integer pid = jsonObject.getInteger("pid");
            String description = jsonObject.getString("description");
            String companyCode = HeaderUtil.getCompanyCode(request,redisService);
            TDept dept = new TDept();
            dept.setDeptName(deptName);
            dept.setPid(pid);
            dept.setCompanyCode(companyCode);
            dept.setDescription(description);
            int t = deptService.insert(dept);
            return ResponseResult.success(ReturnValueConstants.ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.ADD_FAILURE);
        }
    }
    @ApiOperation(value="修改部门", notes="修改一个部门信息",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", required = true),
            @ApiImplicitParam(name = "pid", value = "父节点", required = true),
            @ApiImplicitParam(name = "description", value = "备注", required = true)
    })
    @PostMapping(value = "modDept")
    public ResponseResult modDept(@RequestBody JSONObject jsonObject){
        try {
            logger.info("id为{}的部门已修改",jsonObject.toJSONString());
            Integer id = jsonObject.getInteger("id");
            String deptName = jsonObject.getString("deptName");
            Integer pid = jsonObject.getInteger("pid");
            String description = jsonObject.getString("description");
            TDept tDept = deptService.getDept(id);
            tDept.setDeptName(deptName);
            tDept.setPid(pid);
            tDept.setDescription(description);
            tDept.setId(id);
            deptService.update(tDept);
            return ResponseResult.success(ReturnValueConstants.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(ReturnValueConstants.UPDATE_FAILURE);
        }
    }
}
